package com.framework.utils.pay.youfupay.method;

import com.alibaba.fastjson.JSON;
import com.framework.entity.sys.SysBank;
import com.framework.entity.sys.SysPayment;
import com.framework.exception.RRException;
import com.framework.utils.ali.AliOssUtils;
import com.framework.utils.ali.entity.AliOssDownResult;
import com.framework.utils.pay.suixingfu.horfu.util.Base64Utils;
import com.framework.utils.pay.suixingfu.horfu.util.DESUtils;
import com.framework.utils.pay.suixingfu.horfu.util.JsonUtils;
import com.framework.utils.pay.suixingfu.horfu.util.RSAUtils;
import com.framework.utils.pay.youfupay.entity.ContractModle;
import com.framework.utils.pay.youfupay.entity.RequestMessage;
import com.framework.utils.pay.youfupay.util.Constant;
import com.framework.utils.pay.youfupay.util.FileTool;
import com.framework.utils.pay.youfupay.util.HttpClientHelper;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wq
 * @date 2019/10/9 17:50
 * @description
 */
public class SignContractMethod {


    //static String url = "http://api.clooudnext.com:8280/pctrans-business/clientBusiness/common.do";
    static String url = "http://api.youfupingtai.com/clientBusiness/common.do";
    //static String url = "http://apitest.youfupingtai.com/pctrans-business/clientBusiness/common.do";
    public static void signContract(String secretKey, SysPayment payment, SysBank bank, String identity, String identityHead, String identityEm, String levyId) {
        RequestMessage rm = new RequestMessage();
        rm.setReqId("req" + System.currentTimeMillis());
        rm.setFunCode("6010");
        rm.setMerId(payment.getClientId());
        rm.setVersion("V1.0");

        String detailData=null;
        RequestMessage requestMessage=null;
        try {
            AliOssDownResult resultHead = AliOssUtils.downLoad(identityHead);

            String fileStringOn = FileTool.byte2Hex(FileTool.getBytesByInputStream(resultHead.getInputStream()));

            resultHead.getOssClient().shutdown();

            AliOssDownResult resultEm = AliOssUtils.downLoad(identityEm);

            String fileStringBack = FileTool.byte2Hex(FileTool.getBytesByInputStream(resultEm.getInputStream()));

            resultEm.getOssClient().shutdown();

            rm.setRemark1(fileStringOn);
            rm.setRemark2(fileStringBack);
            String json = getJson(bank, identity, levyId);
            byte[] bs = DESUtils.encrypt(json.getBytes(Constant.CHARSET), secretKey);
            String reqDataEncrypt = Base64Utils.encode(bs);
            rm.setReqData(reqDataEncrypt);
            rm.setSign(RSAUtils.sign(reqDataEncrypt, payment.getPrivateKey()));
            String reqStr = JsonUtils.toJson(rm);
            //System.out.println(reqStr);
            Map<String, Object> map = new HashMap<>();
            map.put("reqJson", reqStr);
            String resData = HttpClientHelper.httpClientPost(url, map, Constant.CHARSET);
            //System.out.println(resData);

            requestMessage = JsonUtils.fromJson(resData, RequestMessage.class);
            byte[] base64bs = Base64Utils.decode(requestMessage.getResData());
            byte[] debs = DESUtils.decrypt(base64bs, secretKey);
            detailData = new String(debs, Constant.CHARSET);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (!Constant.SUCCESS.equals(requestMessage.getResCode())) {
            throw new RRException("9000", requestMessage.getResMsg());
        }

        //System.out.println(detailData);
        Map<String, Object> result = JSON.parseObject(detailData, Map.class);

        if (!Constant.SUCCESS.equals(result.get("resCode"))) {
            throw new RRException("9000", result.get("resMsg").toString());
        }

    }

    private static String getJson(SysBank bank, String identity, String levyId) {

        ContractModle cm = new ContractModle();
        cm.setLevyId(Long.valueOf(levyId));
        cm.setSignType(2);
        cm.setCardNo(bank.getReservedBankNo());
        cm.setIdCard(identity);
        cm.setMobile(bank.getPhone());
        cm.setName(bank.getCardName());

        String json = JsonUtils.toJson(cm);
        return json;
    }

}
